﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Common;
using CNCSoft.ErpApps.TMS.myclass;
using System.Text.RegularExpressions;

namespace CNCSoft.ErpApps.TMS.longGiaoVu
{
    public partial class frmMoLop : Form
    {
        private SqlConnection conn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);
        private DataSet ds = new DataSet();
        private CommonClass mine = new CommonClass();
        private string companyID = GlobalVariable.WSInfo.CompanyID;
        private string userID = GlobalVariable.WSInfo.UserID;

        private bool signWaitingClass = false;
        private bool signReadyClass = false;
        private bool signInProcessClass = false;

        public frmMoLop()
        {
            InitializeComponent();
        }

        private void frmMoLop_Load(object sender, EventArgs e)
        {
            LoadControl();
        }

        private void frmMoLop_Enter(object sender, EventArgs e)
        {
            LoadControl();
        }


        private void btnMoLop_Click(object sender, EventArgs e)
        {
            MoLop();
        }

        #region coding
        private void LoadControl()
        {
            try
            {
                //if (userID.ToLower() == "test")
               // {
                    LoadWaitingClass();
                //}
                //else
                //{
                   LoadReadyClass();
               // }
                //LoadReadyClassAndEndClass();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //private void LoadWaiting()
        //{
        //    throw new NotImplementedException();
        //}

        private void LoadWaitingClass()
        {
            try
            {
                if (signWaitingClass)
                    ds.Tables["TableWaitingClass"].Rows.Clear();
                string sql =
                    @"SELECT
                    MaKhoaHoc, TenKhoaHoc, 
                    CONVERT(VARCHAR, NgayBatDau , 103) AS NgayBatDau, 
                    TrangThai, DoanhThu, 
                    ChiSo,COUNT(EnquiryNumber) AS SiSoThuc
                    FROM DM_KHOAHOC AS A LEFT JOIN Invoice AS B
                    ON A.MaKhoaHoc = B.MaLop
                    WHERE TrangThai IN ('Waiting') AND A.MaDonVi = '" + companyID + @"'
                    GROUP BY A.MaKhoaHoc,TenKhoaHoc,TrangThai,DoanhThu,ChiSo,NgayBatDau
                    ORDER BY NgayBatDau";
                mine.ExecDataAdapter(sql, conn, ds, "TableWaitingClass", gridControlWaitingKhoaHoc);
                signWaitingClass = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadReadyClass()
        {
            try
            {
                if (signReadyClass)
                    ds.Tables["TableReadyClass"].Rows.Clear();
                string sql =
                    @"SELECT
                    MaKhoaHoc, TenKhoaHoc, 
                    CONVERT(VARCHAR, NgayBatDau , 103) AS NgayBatDau, 
                    TrangThai, DoanhThu, 
                    ChiSo,NgayMoLop
                    FROM DM_KHOAHOC
                    WHERE TrangThai = 'Ready' AND MaDonVi = '" + companyID + @"'
                    ORDER BY NgayBatDau";
                mine.ExecDataAdapter(sql, conn, ds, "TableReadyClass", gridControlReadyKhoaHoc);
                signReadyClass = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

//        private void LoadReadyClassAndEndClass()
//        {
//            try
//            {
//                if (signInProcessClass)
//                    ds.Tables["TableInProcessClass"].Rows.Clear();
//                string sql =
//                    @"SELECT
//                    MaKhoaHoc, TenKhoaHoc, 
//                    CONVERT(VARCHAR, NgayBatDau , 103) AS NgayBatDau, 
//                    TrangThai, DoanhThu, 
//                    ChiSo, NgayMoLop
//                    FROM DM_KHOAHOC
//                    WHERE TrangThai IN ('Ready') AND MaDonVi = '" + companyID + @"'
//                    ORDER BY NgayBatDau";
//                mine.ExecDataAdapter(sql, conn, ds, "TableInProcessClass", gridControlReadyKhoaHoc);
//                signInProcessClass = true;
//            }
//            catch (Exception ex)
//            {
//                MessageBox.Show(ex.Message);
//            }
//        }

        //kiem tra khi mo lop
       

        private void MoLop()
        {
            try
            {
                int i = 0;
                string strField = null;
                string choosedvalues = mine.Choose(gridViewWaitingKhoaHoc, strField);

                string sisotinh = mine.Choose(gridViewWaitingKhoaHoc, "ChiSo");
                string[] cutSiSoTinh = sisotinh.Split(new string[] { "('" }, StringSplitOptions.None);
                string[] cutSiSoTinh2 = cutSiSoTinh[1].Split(new string[] { "')" }, StringSplitOptions.None);

                string sisothuc = mine.Choose(gridViewWaitingKhoaHoc, "SiSoThuc");
                string[] cutSiSoThuc = sisothuc.Split(new string[] { "('" }, StringSplitOptions.None);
                string[] cutSiSoThuc2 = cutSiSoThuc[1].Split(new string[] { "')" }, StringSplitOptions.None);

                if (choosedvalues != "()" && Convert.ToInt32(cutSiSoThuc2[0]) >= Convert.ToInt32(cutSiSoTinh2[0]))//so sánh dieu kien ap dung mo lop
                {
                    string sql =
                        @"UPDATE DM_KHOAHOC
                        SET TrangThai = 'Ready', NgayMoLop = GETDATE()
                        WHERE MaKhoaHoc IN " + choosedvalues;
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    try
                    {
                        conn.Open();
                        i = cmd.ExecuteNonQuery();
                    }
                    finally
                    {
                        if (conn != null)
                            conn.Close();
                    }
                    if (i > 0)
                    {
                        MessageBox.Show("Mở lớp thành công");
                        LoadReadyClass();
                        LoadWaitingClass();
                    }
                }
                else
                {
                    MessageBox.Show("Bạn chưa chọn lớp hoặc sĩ số lớp chưa đủ để mở lớp");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        private void btnHuy_Click(object sender, EventArgs e)
        {
            int i = 0;
            string choosedvalues = mine.Choose(gridViewReadyKhoaHoc, null);
            if (choosedvalues != "()")
            {
                string sql =
                            @"UPDATE DM_KHOAHOC
                        SET TrangThai = 'Waiting', NgayMoLop = GETDATE()
                        WHERE MaKhoaHoc IN " + choosedvalues;
                SqlCommand cmd = new SqlCommand(sql, conn);
                try
                {
                    conn.Open();
                    i = cmd.ExecuteNonQuery();
                }
                finally
                {
                    if (conn != null)
                        conn.Close();
                }
                if (i > 0)
                {
                    MessageBox.Show("Hủy mở lớp thành công");
                    LoadReadyClass();
                    LoadWaitingClass();
                }
            }
        }
    }
}
